import Vue from 'vue'
import Vuex from "vuex"

Vue.use(Vuex);

const store = new Vuex.Store({
    // 数据，值必须是一个对象
    state:{
        num:4,
    },
    getters:{
        doubleNum(state){
            return state.num*2;
        }
    },
    mutations:{
        // 每次加一
        increment(state){
            state.num++;
        },
        // 每次加N
        incrementN(state,{n}){
            state.num +=n;
        }
    },
    actions: {
        incrementWait({commit}){
            setTimeout(() => {
                commit("increment")
            }, 1000);
        },
        imcrementNWait({commit},payload){
            setTimeout(() => {
                commit("incrementN",payload)
            }, 1000);
        }
    }
})
export default store;